layui.define('jquery', function(exports) {
    "use strict";
    var $ = layui.$;
    var hint = layui.hint();
    $.ajax({
        url: layui.cache.base + 'checkbox/checkbox.css',
        async: false,
        success: function(res) {
            $('head').append('<style id="chexbox-table-css">' + res + '</style>');
        }
    });
    //layui.link(layui.cache.base + "checkbox/checkbox.css");
    var CheckBox = function(options) {
        this.options = options;
    };
    //初始化
    CheckBox.prototype.init = function(elem) {
        var that = this;
        var options = that.options;
        var nodes = options.data;
        var list = [];
        if (!!options.parseData) {
            layui.each(nodes, function(index, item) {
                var newnodes = typeof options.parseData === 'function' && that.parseData(item);
                if (!!newnodes) {
                    list.push(newnodes);
                }
            });
            if (list != [])
                options.data = list;
        }

        elem.addClass('checkBox'); //添加checkBox样式
        that.checkbox(elem);
    };
    //树节点解析
    CheckBox.prototype.checkbox = function(elem, children) {
        var that = this;
        var options = that.options;
        var nodes = children || options.data;
        layui.each(nodes, function(index, item) {
            //onmouseover="layui.layer.tips(\'' + item.type + '\',this,{tips:2})" <i class="right layui-icon layui-icon-ok"></i>onmouseout="layui.layer.closeAll(\'tips\');"<i class="del"><i class="layui-icon layui-icon-delete"></i></i>
            var li = $(['<li id="' + item.id + '" class="block' + (item.on ? ' on' : '') + '" value="' + item.title + '"><i class="layui-icon layui-icon-username"></i>  ' + item.title, '<i class="choice"><i class="triangle"></i></i><span class="hide">' + (item.on ? '<input type="hidden" name="' + item.title + '" value="' + item.type + '">' : '') + '</span></li>'].join(''));
            elem.append(li);
            //触发点击节点回调
            typeof options.click === 'function' && that.click(li, item);
            //触发删除节点回调
            typeof options.del === 'function' && that.del(li, item);
        });
    };
    CheckBox.prototype.parseData = function(item) {
            var that = this;
            var options = that.options;
            var data = options.parseData(item);
            console.log(data);
            return data;
        }
        //点击节点回调
    CheckBox.prototype.click = function(elem, item) {
        var that = this,
            options = that.options;
        elem.on('click', function(e) {
            elem.toggleClass("on");
            if (elem.hasClass("on")) {
                item.on = true;
                elem.children("span.hide").html('<input type="hidden" name="' + item.title + '" value="' + item.type + '">');
            } else {
                item.on = false;
                elem.children("span.hide").html('');
            }
            layui.stope(e);
            options.click(item);
            that.checkedNumber();
        });
    };
    //点击节点回调
    CheckBox.prototype.del = function(elem, item) {
        var that = this,
            options = that.options;
        elem.children('i.del').on('click', function(e) {
            var index = layer.confirm('确定删除 [' + item.title + '] 吗？', {
                btn: ['删除', '取消']
            }, function() {
                layer.close(index);
                if (options.del(item)) {
                    elem.closest(".block").remove();
                    layui.stope(e);
                }
                that.checkedNumber();
            });
            return false;
        });
    };
    CheckBox.prototype.removeChecked = function() {
        var that = this;
        var options = that.options;
        var elem = $(options.elem);
        var nodes = options.data;
        var list = [];
        layui.each(nodes, function(index, item) {
            if (item.on) {
                list.push(item);
            }
        });
        layui.each(list, function(index, item) {
            if (item.on) {
                var checkbox = $("#" + item.id);
                if (options.del(item)) {
                    checkbox.closest(".block").remove();
                }
            }
        });
        for (var i = 0; i < nodes.length; i++) {
            for (var j = 0; j < list.length; j++) {
                if (nodes[i].id == list[j].id) {
                    nodes.splice(i, 1);
                }
            }
        }
        that.checkedNumber();
    };

    CheckBox.prototype.remove = function(id) {
        var that = this;
        var options = that.options;
        var elem = $(options.elem);
        var nodes = options.data;
        var delitem;
        var i;
        for (var i = 0; i < nodes.length; i++) {
            var item = nodes[i];
            if (item.id == id) {
                item.on = false;
                var checkbox = $("#" + item.id);
                if (options.del(item)) {
                    nodes.splice(i, 1);
                    checkbox.closest(".block").remove();
                }
                return true;
            }
        }
        that.checkedNumber();
    }
    CheckBox.prototype.setChecked = function(id) {
        var that = this;
        var options = that.options;
        var elem = $(options.elem);
        var nodes = options.data;
        layui.each(nodes, function(index, item) {
            if (id == item.id) {
                var checkbox = $("#" + item.id);
                if (!item.on) {
                    checkbox.toggleClass("on");
                    item.on = true;
                    checkbox.children("span.hide").html('<input type="hidden" name="' + item.title + '" value="' + item.type + '">');
                }
                return true;
            }
        });
        that.checkedNumber();
    }
    CheckBox.prototype.checkedAll = function() {
        var that = this;
        var options = that.options;
        var elem = $(options.elem);
        var nodes = options.data;
        layui.each(nodes, function(index, item) {
            item.on = true;
        });
        elem.children("li").remove();
        that.checkbox(elem);
    };
    CheckBox.prototype.unCheckedAll = function() {
        var that = this;
        var options = that.options;
        var elem = $(options.elem);
        var nodes = options.data;
        layui.each(nodes, function(index, item) {
            item.on = false;
        });
        elem.children("li").remove();
        that.checkbox(elem);
    };
    //获取选中节点
    CheckBox.prototype.checkStatus = function() {
        var that = this;
        var options = that.options;
        var elem = $(options.elem);
        var nodes = options.data;
        var list = [];
        layui.each(nodes, function(index, item) {
            if (item.on) {
                var d = { id: item.id, title: item.title };
                list.push(d);
            }
        });
        return list;
    };
    CheckBox.prototype.checkedNumber = function() {
        var that = this;
        var options = that.options;
        var elem = $(options.elem);
        var nodes = options.data;
        var list = that.checkStatus();
        options.checked(list.length);
    };
    /** 对外提供的方法 */
    var chexkboxTb = {
        render: function(options) {
            var checkbox = new CheckBox(options = options || {});
            var elem = $(options.elem);
            if (!elem[0]) {
                return hint.error('layui.checkbox 没有找到' + options.elem + '元素');
            }
            elem.children("li").remove();
            checkbox.init(elem);
            return checkbox;
        }

    };
    //暴露接口
    exports('checkbox', chexkboxTb);
});